/**
 * @ClassName NavigationBarData
 * @Author 谭金诚
 * @Date 2021/5/515:37
 */
package com.pro.control;

import com.pro.entity.*;
import com.pro.service.NavigationBarService;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/navigationBarData")
public class NavigationBarController {

    @Autowired
    private NavigationBarService service;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private RedisCache redisCache;






    /**
     * @ClassName NavigationBarData
     * @Author 谭金诚
     * @Date 2021/11/9
     */
    @RequestMapping("/getNavigationBarData")
    @ResponseBody
    public JsonData getNavigationBarData(String type){
            List<JsMenu> nav = new ArrayList<>();
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String name = authentication.getName();
            UserDetails userDetails = userDetailsService.loadUserByUsername(name);
            User cacheObject = (User) redisCache.getCacheObject("user:" + userDetails.getUsername());
            List<Integer> deptAndUser = service.getDeptAndUser(cacheObject.getUserId());
            if(deptAndUser.size() == 1){
                nav = (List<JsMenu>) redisCache.getCacheObject("deptId:" + cacheObject.getDeptId());
                if(nav == null){
                    nav = service.getNav(deptAndUser.get(0));
                    redisCache.setCacheObject("dept:"+deptAndUser.get(0),nav);
                }
            }else{
                Set<JsMenu> set = new HashSet<>();
                for (Integer integer : deptAndUser) {
                    List<JsMenu> cacheObject1 = (List<JsMenu>) redisCache.getCacheObject("deptId:" + integer);
                    if(cacheObject1  == null){
                        cacheObject1 = service.getNav(integer);
                        redisCache.setCacheObject("dept:"+integer,cacheObject1);
                    }
                    set.addAll(cacheObject1);
                }
                nav.addAll(set);
            }

            for (JsMenu navDatum : nav) {
                if(navDatum.getParentId() == 0){
                    navDatum.setChildren(getMenu(nav,navDatum.getMenuId()));
                    List<JsMenu> navDatumData = navDatum.getChildren();
                    for (JsMenu navDatumDatum : navDatumData) {
                        navDatumDatum.setChildren(getMenu(nav,navDatumDatum.getMenuId()));
                    }
                }
            }

            ListIterator<JsMenu> jsMenuListIterator = nav.listIterator();
            while (jsMenuListIterator.hasNext()) {
                boolean b = jsMenuListIterator.hasNext();
                if(b){
                    if(jsMenuListIterator.next().getParentId() != 0){
                        jsMenuListIterator.remove();
                    }
                }
            }
        return nav != null ? JsonData.buildSuccess(nav) : JsonData.buildError("服务端错误");
    }
    private List<JsMenu> getMenu(List<JsMenu> menus,Integer menuId){
        List<JsMenu> cMenus = new ArrayList<>();
        for (JsMenu menu : menus) {
            if(menu.getParentId() == menuId){
                cMenus.add(menu);
            }
        }
        return cMenus;
    }
    /**
     * @ClassName JsUser
     * @Author 谭金诚
     * @Date 2021/11/6
     */
    @RequestMapping("/getJsUser")
    @ResponseBody
    public JsonData getJsUser(Integer id){
        JsUser jsUser = service.getJsUser(id);
        return jsUser != null ? JsonData.buildSuccess(jsUser) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName LoginRecord
     * @Author 谭金诚
     * @Date 2021/11/6
     */
    @RequestMapping("/getLoginUser")
    @ResponseBody
    public JsonData getLoginUser(Integer id){
        List<LoginRecord> loginUser = service.getLoginUser(id);
        return loginUser != null ? JsonData.buildSuccess(loginUser) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName OrderQuantity
     * @Author 谭金诚
     * @Date 2021/11/15
     */
    @RequestMapping("/getOrderQuantityStatus")
    @ResponseBody
    public JsonData getOrderQuantityStatus(Integer id){
        OrderQuantity orderQuantityStatus = service.getOrderQuantityStatus(id);
        return orderQuantityStatus != null ? JsonData.buildSuccess(orderQuantityStatus) : JsonData.buildError("服务端错误");
    }



}
